package com.example.service.Impl;

import com.example.entity.Article;
import com.example.mapper.ArticleMapper;
import com.example.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Map<String, Object> getArticleByPage(Integer pageNum, Integer pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<Article> articles = articleMapper.selectArticlesByPage(offset, pageSize);
        int total = articleMapper.countArticles();
        Map<String, Object> result = new HashMap<>();
        result.put("data", articles);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        return result;
    }

    @Override
    public Map<String, Object> searchArticlesByTitle(Integer pageNum, Integer pageSize, String titlePart) {
        int offset = (pageNum - 1) * pageSize;
        List<Article> articles = articleMapper.selectArticlesByTitlePart(offset, pageSize, titlePart);
        int total = articleMapper.countArticlesByTitlePart(titlePart);
        Map<String, Object> result = new HashMap<>();
        result.put("data", articles);
        result.put("total", total);
        result.put("pageNum", pageNum);
        result.put("pageSize", pageSize);
        return result;
    }

    @Override
    public Article getArticleById(Integer id) {
        return articleMapper.selectArticleById(id);
    }
}
